package com.vogue.supplymall.upload.controller;

import com.vogue.supplymall.common.domain.dto.ControllerResultInfo;
import com.vogue.supplymall.common.domain.dto.UploadInfo;
import com.vogue.supplymall.common.exception.RemoveUploadException;
import com.vogue.supplymall.common.exception.UploadException;
import com.vogue.supplymall.upload.domain.service.UploadService;
import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;

/**
 * The type Pay voucher upload controller.
 * 上传/删除订单的付款凭证controller
 *
 * @aouthor: zhangliang
 * @date: Create in 2018-04-09
 */
@RestController
public class PayVoucherUploadController extends BaseController {
    /**
     * The constant log.
     */
    public static final Logger LOGGER_PAYVOUCHERUPLOADCONTROLLER = LoggerFactory.getLogger(PayVoucherUploadController.class);
    @Resource
    private UploadService uploadService;

    /**
     * Upload refund img response entity.
     * 上传支付凭证
     *
     * @param appver             the appver
     * @param uploadResourceInfo the upload resource info
     * @return the response entity
     */
    @RequestMapping(value = "/is/{appver}/upload/order-payvoucher/upload", method = RequestMethod.POST)
    public ResponseEntity<Object> uploadPayVoucherImg(@PathVariable("appver") String appver,
                                                      @RequestBody UploadInfo uploadResourceInfo) {
        LOGGER_PAYVOUCHERUPLOADCONTROLLER.info("upload pay voucher image start >>>>>> ");
        if (null == uploadResourceInfo.getDataid() || null == uploadResourceInfo.getPicData()
                || null == uploadResourceInfo.getExtension() || null == uploadResourceInfo.getFileName()) {
            throw new UploadException("");
        }

        ControllerResultInfo resultInfo = new ControllerResultInfo();
        resultInfo.setResult(true);
        uploadResourceInfo.setModuleName("orderPayVoucher");
        resultInfo.setData(uploadService.uploadResource(uploadResourceInfo));
        LOGGER_PAYVOUCHERUPLOADCONTROLLER.info(" <<<<<< upload pay voucher image end");
        return new ResponseEntity<>(resultInfo, HttpStatus.OK);
    }

    /**
     * Remove refund img response entity.
     *
     * @param appver             the appver
     * @param uploadResourceInfo the upload resource info
     * @return the response entity
     */
    @RequestMapping(value = "/is/{appver}/upload/order-payvoucher/remove", method = RequestMethod.POST)
    public ResponseEntity<Object> removePayVoucherImg(@PathVariable("appver") String appver,
                                                      @RequestBody UploadInfo uploadResourceInfo) {
        LOGGER_PAYVOUCHERUPLOADCONTROLLER.info("remove pay voucher image start >>>>>> ");
        if (StringUtils.isEmpty(uploadResourceInfo.getDeletePath())) {
            throw new RemoveUploadException("");
        }
        ControllerResultInfo resultInfo = new ControllerResultInfo();
        resultInfo.setResult(true);
        uploadResourceInfo.setModuleName("orderPayVoucher");
        resultInfo.setData(uploadService.removeResource(uploadResourceInfo));
        LOGGER_PAYVOUCHERUPLOADCONTROLLER.info(" <<<<<< remove pay voucher image end");
        return new ResponseEntity<>(resultInfo, HttpStatus.OK);
    }

}
